﻿using System;
using System.Linq;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;

namespace Sinbiota.Web.Controllers
{
    public class KeywordController : BaseController
    {
        private readonly IKeywordPersistenceServices _keywordPersistenceServices;

        public KeywordController(IKeywordPersistenceServices keywordPersistenceServices)
        {
            _keywordPersistenceServices = keywordPersistenceServices;
        }

        //
        // GET: /Keyword/
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Index()
        {
            return View(_keywordPersistenceServices.FindAll());
        }
        // POST (search)
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(_keywordPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }
        //
        // GET: /Keyword/Details/5
        [AdminPermission]
        [ImportMessagesFromTempData]
        public ActionResult Details(int id)
        {
            var keyword = _keywordPersistenceServices.FindById(id);
            return keyword == null ? View("NotFound") : View(keyword);
        }

        //
        // GET: /Keyword/Create
        [AdminPermission]
        [ImportMessagesFromTempData]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Keyword/Create
        [AdminPermission]
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Create(Keyword keyword)
        {
            var newKeyword = new Keyword();
            TryUpdateModel(newKeyword);
            if (ModelState.IsValid)
            {
                _keywordPersistenceServices.Save(newKeyword);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(keyword);
        }

        //
        // GET: /Keyword/Edit/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Edit(int id)
        {
            var keyword = _keywordPersistenceServices.FindById(id);
            return keyword == null ? View("NotFound") : View(keyword);
        }

        //
        // POST: /Keyword/Edit/5
        [AdminPermission]
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Edit(int id, Keyword keyword)
        {
            var editKeyword = _keywordPersistenceServices.FindById(id);
            TryUpdateModel(editKeyword);
            if (ModelState.IsValid)
            {
                _keywordPersistenceServices.Update(editKeyword);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(keyword);
        }

        //
        // GET: /Keyword/Delete/5
        [AdminPermission]
        [ImportMessagesFromTempData]
        public ActionResult Delete(int id)
        {
            var keyword = _keywordPersistenceServices.FindById(id);
            return keyword == null ? View("NotFound") : View(keyword);
        }

        //
        // POST: /Keyword/Delete/5
        [AdminPermission]
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var deleteKeyword = _keywordPersistenceServices.FindById(id);
            if (deleteKeyword == null) return View("NotFound");
            _keywordPersistenceServices.Delete(deleteKeyword);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }
        //AJAX
        public JsonResult AutoComplete(string term, int count = 30)
        {
            return Json(_keywordPersistenceServices.FindByNameLike(term).Select(t => t.Name).Take(count), JsonRequestBehavior.AllowGet);
        }

    }
}
